<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\OrderSupplyAction;
use App\Admin\Repositories\WxOrder;
use App\Jobs\Order\OrderDeliverInfoManage;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxOrderController extends AdminController
{

    protected $status_arr = [0 => '未付款', 1 => '已付款', 2 => '已退款', 3=>'待小程序发货', 4=>'小程序发货中', 5=>'待小程序确定收货', 6=>'小程序确定收货中'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxOrder('user'), function (Grid $grid) {
            $grid->simplePaginate();
            $grid->model()->orderBy('id','desc');
            $grid->quickSearch(['id','user_id','user.user_name', 'order_number', 'order_serial_number'])->placeholder('搜索ID，用户ID，用户名字，商户流水号，微信支付流水号...')->width(50);
            $grid->column('id')->sortable();
            $grid->column('users', '头像')->display(function ($v) {
                return '<img src="' . ($this->user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('user')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '';
            })->width('100px');
            $grid->column('order_type')->using(FieldUtils::getOrderTypes())->label([
                0 => 'success',
                1 => 'pink',
                2 => 'primary',
                3 => 'green',
                4 => 'pink',
                5 => 'pink',
                6 => 'pink',
                7 => 'pink',
                8 => 'green',
                9 => 'black'
            ]);
            $grid->column('order_information')->limit(8);
            $grid->column('order_price');
            $grid->column('order_pay_price')->label('success');
            $grid->column('order_number')->limit(8);
            $grid->column('order_serial_number')->limit(8);
            $grid->column('order_state')->display(function ($v){
                if($v == 3 && !_empty_($this->order_serial_number)){
                    OrderDeliverInfoManage::dispatch('upload', $this->id);
                }else if($v == 5 && !_empty_($this->order_serial_number)){
                    OrderDeliverInfoManage::dispatch('query', $this->id);
                }
                return $v;
            })->using($this->status_arr)->label([
                0 => 'red',
                1 => 'success',
                2 => 'primary',
            ]);
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('order_type')->select(FieldUtils::getOrderTypes());
                $filter->equal('order_number');
                $filter->equal('order_state')->select($this->status_arr);
            });
            $grid->disableCreateButton();
//            $grid->disableActions();
            // 禁用编辑按钮
            $grid->disableEditButton();
            // 禁用详情按钮
            $grid->disableViewButton();
            $grid->disableBatchDelete();
            $grid->batchActions(function ($batch) {
                $batch->add(new OrderSupplyAction());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxOrder(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('order_type');
            $show->field('order_information');
            $show->field('order_price');
            $show->field('order_pay_price');
            $show->field('order_number');
            $show->field('order_serial_number');
            $show->field('order_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxOrder(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('order_type');
            $form->text('order_information');
            $form->text('order_price');
            $form->text('order_pay_price');
            $form->text('order_number');
            $form->text('order_serial_number');
            $form->text('order_state');


            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });


        });
    }
}
